package manager 
{
	import base.ModuleBase;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import Core.config.ModuleCore.config.
	import Elegant.utils.core.Singleton;
	import Elegant.utils.InitObject;
	import flash.display.Sprite;
	
	/**
	 * 界面管理,控制模块的显示与否
	 * 
	 */
	public final class InterfaceManager 
	{
		public var topLayer:Sprite;
		public var middleLayer:Sprite;
		public var buttomLayer:Sprite;
		
		private static var _rootSprite:Sprite = new Sprite();
		private static var _currentModule:ModuleBase;
		
		public function InterfaceManager() 
		{
			topLayer = new Sprite();
			middleLayer = new Sprite();
			buttomLayer = new Sprite();
			
			_rootSprite.addChild(buttomLayer);
			_rootSprite.addChild(middleLayer);
			_rootSprite.addChild(topLayer);
		}
		
		public static function get getInstance():InterfaceManager
		{
			return Singleton.getInstanceOrCreate(InterfaceManager);
		}
		
		public static function centerModule(target:ModuleBase):void
		{
			target.x = (_rootSprite.stage.stageWidth - (target.moduleWidth >= 0 ? target.moduleWidth : target.width)) >> 1;
			target.y = (_rootSprite.stage.stageHeight - (target.moduleHeight >= 0 ? target.moduleHeight : target.height)) >> 1;
		}
		
		/**
		 * 打开模块
		 * @param	name
		 * @param	data	该data可以在各个模块的主类中直接使用,如果ExampleMain.data;
		 * 					hideMap
		 */
		public function openModule(name:String, data:Object = null):void
		{
			var module:ModuleBase = ResourceManager.getModule(name);
			
			if (module == null)
			{
				//由ModuleCore.config.置
				var moduleItem:ModuleItem = ModuleCore.config.name.toUpperCase()];
				ResourceManager.loadSWFs(moduleItem.resources, { onComplete:onModuleLoaded, name:moduleItem.name, moduleName:name, data:data } );
			} else {
				onModuleLoaded( { e:module, data:data } );
			}
		}
		
		/**
		 * 关闭模块
		 * @param	closeFromModuleself		为true时不会调用当前module的close方法,以免死循环
		 */
		public function closeModule(closeFromModuleSelf:Boolean = false):void
		{
			//若不是由模块调用的此方法,则先调用模块的close
			if(closeFromModuleSelf)
			{
				topLayer.removeChild(_currentModule);
				_currentModule = null;
				
				if (WorldMap.getInstance.isDisposed)
					CatalogGirlsMain.instance.buildScene();
			} else {
				_currentModule.close();
			}
		}
		
		//--------------------------------------------------
		private function onModuleLoaded(objectVars:Object):void
		{
			var module:ModuleBase = objectVars.e is ModuleBase ? ModuleBase(objectVars.e) : ResourceManager.getModule(objectVars.moduleName);
			if (_currentModule != null)
				closeModule();
			
			module.data = objectVars.data;
			if (InitObject.getBoolean(objectVars.data, "hideMap", false))
			{
				delete objectVars.hideMap;
				WorldMap.getInstance.dispose();
			}
			
			centerModule(module);
			topLayer.addChild(module);
			
			module.open();
			_currentModule = module;
		}
		
		//-------------------getter / setter-----------------
		
		public static function get currentModule():ModuleBase
		{
			return _currentModule;
		}
		
		public static function get rootSprite():Sprite
		{
			return _rootSprite;
		}
	}
}